package com.school.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.school.pojo.File;
import com.school.service.FileService;
import com.school.util.FileUtil;
import com.school.util.Result;
import com.school.util.Tool;

@RequestMapping("/file")
@RestController
public class FileController implements Tool<File>{

	@Autowired
	private FileService fileService;

	@Override
	@RequestMapping("/findAll")
	public Result<File> findAll(int page, int pageSize, File t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	@RequestMapping("/findbyId")
	public Result<File> findbyId(int id) {
		File file= fileService.findbyId(id);
		Result<File> result=new Result<File>();
		result.setData(file);
		return result;
	}

	@Override
	public int add(File t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int edit(File t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int del(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delbatch(int[] id) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@RequestMapping("/upload")
	public Result<File> upload(MultipartFile file){
		//把图片添加到文件夹中
		String url=FileUtil.getFileuploading(file, "D:/uploads/grade_admin/");
		File file2=new File();
		file2.setUrl(url);
		
		//把图片路径储存到数据库中
		int row=fileService.upload(file2);
		
		File files=new File();
		//file2.getFile_id()储存图片路径返回的id
		files.setFile_id(file2.getFile_id());
		files.setUrl(url);
		
		Result<File> result=new Result<File>();
		result.setData(files);
		return result;
	}
	
	@RequestMapping("/eupload")
	public Map<String, Object> eupload(MultipartFile file){
		//把图片添加到文件夹中
		String url=FileUtil.getFileuploading(file, "D:/uploads/grade_admin/");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("url", url);
		map.put("code", 0);
		map.put("msg", "");
		return map;
	}
}
